#include "vtsradarmanagerwidget.h"
#include "../Radarmanager/VtsRadar/VtsRadar.h"
#include <boost/bind.hpp>
#include <qmetatype.h>
#include "../../config/app_config.h"

VtsRadarManagerWidget::VtsRadarManagerWidget(QWidget *parent)
    : RadarWidgetBase(parent)
{
    ui.setupUi(this);

    qRegisterMetaType<std::string>("std::string");

    connect(ui.horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(SliderValueChanged(int)));
    connect(ui.doubleSpinBox_20, SIGNAL(valueChanged(double)), this, SLOT(AzimuthModifyChanged(double)));
    connect(ui.doubleSpinBox_23, SIGNAL(valueChanged(double)), this, SLOT(RangeModifyChanged(double)));
    connect(ui.doubleSpinBox_22, SIGNAL(valueChanged(double)), this, SLOT(MaxWaveChanged(double)));
    connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(SetRotatingSpeed()));
    connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(WorkFre(int)));

    connect(ui.radioButton, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_2, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_3, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_4, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_5, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_6, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_7, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_8, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_9, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_10, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_11, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_12, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_13, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_14, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_15, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_16, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));
    connect(ui.radioButton_17, SIGNAL(clicked()), this, SLOT(RadioButtonClicked()));

    connect(ui.checkBox_2, SIGNAL(clicked(bool)), this, SLOT(RadarShader(bool)));
    connect(ui.checkBox_3, SIGNAL(clicked(bool)), this, SLOT(RadarShader(bool)));
    connect(ui.checkBox_4, SIGNAL(clicked(bool)), this, SLOT(RadarShader(bool)));
    connect(ui.checkBox_5, SIGNAL(clicked(bool)), this, SLOT(RadarShader(bool)));
    connect(ui.checkBox_6, SIGNAL(clicked(bool)), this, SLOT(RadarShader(bool)));
    connect(ui.checkBox_7, SIGNAL(clicked(bool)), this, SLOT(RadarShader(bool)));
    connect(ui.checkBox_8, SIGNAL(clicked(bool)), this, SLOT(RadarShader(bool)));
    connect(ui.checkBox_9, SIGNAL(clicked(bool)), this, SLOT(RadarShader(bool)));
}

void VtsRadarManagerWidget::Init()
{
    ui.doubleSpinBox_20->setValue(sln_app_config::instance().get_radar_azimuth_modify());
    ui.doubleSpinBox_23->setValue(sln_app_config::instance().get_radar_range_modify());
    ui.doubleSpinBox_22->setValue(sln_app_config::instance().get_radar_maxwave());
}

VtsRadarManagerWidget::~VtsRadarManagerWidget()
{
    
}

void VtsRadarManagerWidget::RadioButtonClicked()
{
    QRadioButton *btn = (QRadioButton*)sender();

    int flag = btn->statusTip().toInt();

    switch (flag)
    {
    case 1:

        break;
    case 3:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Servo(false, 0));
        break;
    case 4:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Servo(true, ui.spinBox->value()));
        break;
    case 5:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Transmit(false));
        break;
    case 6:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Transmit(true));
        break;
    case 7:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Range(0));
        break;
    case 8:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Range(1));
        break;
    case 9:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Range(2));
        break;
    case 10:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Range(3));
        break;
    case 11:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Range(4));
        break;
    case 12:
        m_radar->DataUdp()->SendMsg(m_radar_controller.ClutterSuppression(false));
        break;
    case 13:
        m_radar->DataUdp()->SendMsg(m_radar_controller.ClutterSuppression(true));
        break;
    case 14:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Stc(0));
        break;
    case 15:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Stc(1));
        break;
    case 16:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Stc(2));
        break;
    case 17:
        m_radar->DataUdp()->SendMsg(m_radar_controller.Stc(3));
        break;
    }
}

void VtsRadarManagerWidget::MsgSlot(const std::string& str, unsigned int radar_key)
{
    if (str.size() != sizeof(VtsRadarStatus))
    {
        return;
    }

    VtsRadarStatus *data = (VtsRadarStatus*)(str.c_str());

    if (data->flag != 0x7e7e)
    {
        return;
    }

    ui.comboBox_2->setCurrentIndex(data->pow_flag_1);
    ui.comboBox_3->setCurrentIndex(data->pow_flag_2);
    ui.comboBox_4->setCurrentIndex(data->pow_flag_3);
    ui.comboBox_5->setCurrentIndex(data->pow_flag_4);
    ui.comboBox_6->setCurrentIndex(data->pow_flag_5);
    ui.comboBox_7->setCurrentIndex(data->pow_flag_6);
    ui.comboBox_8->setCurrentIndex(data->pow_flag_7);
    ui.comboBox_9->setCurrentIndex(data->pow_flag_9);
    ui.comboBox_10->setCurrentIndex(data->pow_flag_10);
    ui.comboBox_11->setCurrentIndex(data->servo_flag_1);
    ui.comboBox_12->setCurrentIndex(data->servo_flag_2);
    ui.comboBox_13->setCurrentIndex(data->servo_flag_3);
    ui.comboBox_14->setCurrentIndex(data->servo_flag_4);
    ui.comboBox_15->setCurrentIndex(data->servo_flag_5);
    ui.comboBox_16->setCurrentIndex(data->servo_flag_9);
    ui.comboBox_17->setCurrentIndex(data->amp_flag_1);
    ui.comboBox_18->setCurrentIndex(data->amp_flag_2);
    ui.comboBox_19->setCurrentIndex(data->dsp_status);
    //ui.comboBox_20->setCurrentIndex(data->pow_flag_2);

    ui.doubleSpinBox_2->setValue(data->current);
    //ui.doubleSpinBox_3->setValue()
    ui.doubleSpinBox->setValue(data->antenna_speed * 0.2);
}

void VtsRadarManagerWidget::SliderValueChanged(int val)
{
    m_radar->DataUdp()->SendMsg(m_radar_controller.Mgc(val));
}

void VtsRadarManagerWidget::SetRotatingSpeed()
{
    m_radar->DataUdp()->SendMsg(m_radar_controller.Servo(true, ui.spinBox->value()));
}

void VtsRadarManagerWidget::WorkFre(int val)
{
    m_radar->DataUdp()->SendMsg(m_radar_controller.WorkFreq(val));
}

void VtsRadarManagerWidget::MaxWaveChanged(double val)
{
    ((VtsRadar*)m_radar)->SetMaxWave(val);

    sln_app_config::instance().set_radar_maxwave(val);
}

void VtsRadarManagerWidget::RadarShader(bool val)
{
    unsigned char v = val ? 1 : 0;

    QCheckBox *btn = (QCheckBox*)sender();

    int flag = btn->toolTip().toInt();

    double first_val, second_val;

    switch (flag)
    {
    case 1:
        GetAngle(ui.doubleSpinBox_4, ui.doubleSpinBox_5, first_val, second_val);
        break;
    case 2:
        GetAngle(ui.doubleSpinBox_6, ui.doubleSpinBox_7, first_val, second_val);
        break;
    case 3:
        GetAngle(ui.doubleSpinBox_8, ui.doubleSpinBox_9, first_val, second_val);
        break;
    case 4:
        GetAngle(ui.doubleSpinBox_10, ui.doubleSpinBox_11, first_val, second_val);
        break;
    case 5:
        GetAngle(ui.doubleSpinBox_12, ui.doubleSpinBox_13, first_val, second_val);
        break;
    case 6:
        GetAngle(ui.doubleSpinBox_14, ui.doubleSpinBox_15, first_val, second_val);
        break;
    case 7:
        GetAngle(ui.doubleSpinBox_16, ui.doubleSpinBox_17, first_val, second_val);
        break;
    case 8:
        GetAngle(ui.doubleSpinBox_18, ui.doubleSpinBox_19, first_val, second_val);
        break;
    }

    m_radar->DataUdp()->SendMsg(m_radar_controller.Shader(flag, val, first_val, second_val));
}

void VtsRadarManagerWidget::GetAngle(QDoubleSpinBox *first, QDoubleSpinBox *second, double &first_val, double &second_val)
{
    first_val = first->value();
    second_val = second->value();
}